package sample;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

import java.net.URL;
import java.util.ResourceBundle;

public class SearchRoomTtblePageController implements Initializable {

/*
==============================================================定义fxml文件中元素——————开始===========================================================
 */


    @FXML
    private Button searchRommTable;

    @FXML
    private Button SearchTecherTable;

    @FXML
    private Button ClassNote;
    @FXML
    private ChoiceBox<String> CampusSelected;

    @FXML
    private ChoiceBox<String> BuildingSelected;

    @FXML
    private ChoiceBox<String> roomSelected;

    @FXML
    private Button search;

    @FXML
    private TableView<CouseList> Ttble;
    @FXML
    private TableColumn<CouseList, String> Monday;

    @FXML
    private TableColumn<CouseList, String> Tuesday;

    @FXML
    private TableColumn<CouseList, String> Wednesday;

    @FXML
    private TableColumn<CouseList, String> Thursday;

    @FXML
    private TableColumn<CouseList, String> Friday;

    @FXML
    private TableColumn<CouseList, String> Saturday;

    @FXML
    private TableColumn<CouseList, String> Sunday;



/*
==================================================================定义fxml元素——————结束==============================================================
 */

    String campusSelected,buildingSelected,RoomSelected;
    ObservableList<CouseList> list= FXCollections.observableArrayList();//用于一条条展示课表的列表

    private String[] campus={"江安","望江","华西"};
    private String[][] buildings={{"一教A座","一教B座","一教C座","一教D座","综合楼B座","综合楼C座","文科楼一区","文科楼二区",
                                    "文科楼三区","工程训练中心","法学大楼附楼","艺术实验教学中心","实验室","体育场","建环大楼","艺术大楼","法学大楼主楼",
                                    "灾后重建与管理学院A区","灾后重建与管理学院C区","匹兹堡学院","药学院","制造学院","i创街","文科类四区","江安校区体育馆",
                                    "水电学院","卓越工程师教学训练中心","虚拟教学楼","体能训练中心","多学科交叉研究创新大楼"},
                                  {"东二教","东三教","基教楼A座","基教楼C座","研究生院"},
                                  {"九教","十教"}};
    private String[][] rooms={{"A101","A102","A103","A104","A105","A106","A107","A108","A109","A110","A111","A112","A118","A201","A203","A205","A207"
            ,"A208","A209","A210","A211","A212","A214","A218","A220","A222","A301","A303","A305","A306","A307","A308","A309","A310","A312","A316","A318"
            ,"A320","A401","A403","A405","A406","A407","A408","A409","A410","A411","A412","A416","A420","A501","A503","A505","A506","A507","A508","A509"
            ,"A510","A511","A512","A513","A516","A518","A601","A603","A606","A607","A608","A609","A610","A611","A612","A613","A616","A618","水上报告厅"
            ,"A209A","A209B","A209C","A209D","A309A","A309B","A309C","A309D","A409A","A409B","A409C","A409D","A511A","A511B","A511C","A511D"}, //一教A的所有教室
            {"B101","B102","B103","B104","B105","B107","B201","B202","B203","B204","B205","B207","B301","B302","B303","B304","B305","B307"
                    ,"B401","B402","B403","B404","B405","B407","B501","B502","B503","B504","B505","B507"} //一教B的所有教室
    };

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        Ttble.setVisible(false); //先让表格不可见
        CampusSelected.getItems().addAll(campus);
        CampusSelected.setOnAction(this::getCampusSelected); //得到选中了哪个校区
        BuildingSelected.setOnAction(this::getBuildingSelected); //得到选中了哪些教学楼

        //String campusSelected=CampusSelected.getSelectionModel().getSelectedItem(); 这个可以得到choiceBox的结果


        //在这个地方把表格的每一个column的名字设置到对应的名字，由于存在CourseList类，程序会到CourseList类中按照这里设定的名字寻找get函数，得到要打印的值
        Monday.setCellValueFactory(new PropertyValueFactory<>("MonC"));
        Tuesday.setCellValueFactory(new PropertyValueFactory<>("TueC"));
        Wednesday.setCellValueFactory(new PropertyValueFactory<>("WedC"));
        Thursday.setCellValueFactory(new PropertyValueFactory<>("ThurC"));
        Friday.setCellValueFactory(new PropertyValueFactory<>("FriC"));
        Saturday.setCellValueFactory(new PropertyValueFactory<>("SatC"));
        Sunday.setCellValueFactory(new PropertyValueFactory<>("SunC"));

        Ttble.setItems(list);

    }

    public void getCampusSelected(ActionEvent event) //得到选中了哪个校区后决定展示哪些教学楼
    {
        BuildingSelected.getItems().clear();//防止重复添加
        campusSelected=CampusSelected.getValue(); //得到选择了哪个校区
        if(campusSelected.equals("江安")) BuildingSelected.getItems().addAll(buildings[0]);
        else if(campusSelected.equals("望江")) BuildingSelected.getItems().addAll(buildings[1]);
        else BuildingSelected.getItems().addAll(buildings[2]);
    }


    public void getBuildingSelected(ActionEvent event) //得到选中了哪个教学楼后决定展示哪些教室
    {
        roomSelected.getItems().clear();//防止重复添加
        buildingSelected=BuildingSelected.getValue();
        if(buildingSelected.equals("一教A座")) roomSelected.getItems().addAll(rooms[0]);
        else if(buildingSelected.equals("一教B座")) roomSelected.getItems().addAll(rooms[1]);
    }


    @FXML
    void getRoomTtble(ActionEvent event) {
        RoomSelected=roomSelected.getValue();
        list.clear();
        String[] MonCou= MysqlConn.getRoomSchedule(campusSelected,buildingSelected,RoomSelected,"1");
        String[] TueCou= MysqlConn.getRoomSchedule(campusSelected,buildingSelected,RoomSelected,"2");
        String[] WedCou= MysqlConn.getRoomSchedule(campusSelected,buildingSelected,RoomSelected,"3");
        String[] ThurCou= MysqlConn.getRoomSchedule(campusSelected,buildingSelected,RoomSelected,"4");
        String[] FriCou= MysqlConn.getRoomSchedule(campusSelected,buildingSelected,RoomSelected,"5");
        String[] SatCou= MysqlConn.getRoomSchedule(campusSelected,buildingSelected,RoomSelected,"6");
        String[] SunCou= MysqlConn.getRoomSchedule(campusSelected,buildingSelected,RoomSelected,"7");

        //将信息传入list。因为javafx中tableview是按行来呈现，结合川大课表的特点，我设置了一个长度为10的数组，每个数组代表一小节的课
        //将每一天的第0到10节课一次传入list
        for(int i=0;i<10;i++)
        {
            list.add(new CouseList(MonCou[i],TueCou[i],WedCou[i],ThurCou[i],FriCou[i],SatCou[i],SunCou[i]));
        }
        Ttble.setVisible(true);
    }
        /*
=================================================================以下是界面转换====================================================================
     */

    @FXML
    void SwitchToNotePage(ActionEvent event) {
        SceneManager.SwitchToNotePage(event);
    }

    @FXML
    void SwitchToSearchRoomTtblePage(ActionEvent event) {
        SceneManager.SwitchToSearchRoomTtblePage(event);
    }

    @FXML
    void SwitchToSearchTeacherTtblePage(ActionEvent event) {
        SceneManager.SwitchToSearchTeacherTtblePage(event);
    }

    @FXML
    void showSchedulePage(ActionEvent event) {
        SceneManager.SwitchToSchedulePage(event);
    }

}
